import instance from '@/assets/js/utils'
/*ע��vux��� */
import {
  Alert,
  Cell,
  CellBox,
  CheckIcon,
  ConfirmPlugin,
  Datetime,
  Grid,
  GridItem,
  Group,
  Loading,
  LoadMore,
  Popup,
  PopupPicker,
  PopupHeader,
  PopupRadio,
  Picker,
  Tab,
  TabItem,
  Toast,
  ToastPlugin,
  TransferDom,
  XCircle,
  XHeader,
  XTextarea,
  XButton,
  LoadingPlugin,
  Swiper,
  SwiperItem,
  XProgress,
  AlertPlugin
} from 'vux'

/*ע���Զ������ */
import accordion from '@/components/accordion'
import DeptTree from '@/components/tree/deptTree'
import detailTable from '@/components/table/detailTable'
import SideBar from '@/components/sidebar/sideBar'
import SimpleSearch from '@/components/search/simpleSearch'
import PersonTree from '@/components/tree/personTree'
import PhotoUpload from '@/components/file-upload/photoUpload'
import PictureUpload from '@/components/file-upload/pictureUpload'
import defaultButton from '@/components/defaultButton'
import ChooseProject from '@/components/chooseProject/chooseProject'
import SelectProject from '@/components/chooseProject/selectProject'
import ProjectRoot from '@/components/chooseProject/projectRoot'
//import panel from '@/components/pull-down'
/*����scroller��� */
import VueScroller from 'vue-scroller'
/*����svgIcon*/
import Icon from 'vue-svg-icon/Icon'
import store from '@/store'

/*����utils���� */
import {
  getReqUrl,
  downloadFile,
  getTaskParams,
  getRiskParams,
  guid,
  pattern,
  toTree,
  signMD5,
  judgeDeviceType,
  checkNetWork
} from './utils'
// 开发时DEV-FLAG = true
// 打包时为！true
// let DEV_FLAG = !true
let DEV_FLAG = false

// let prodUrl = process.env.BASE_URL
// let tokenUrl = process.env.TOKEN_URL
let prodUrl = 'http://jgbim.qhfct.com:9000/'
let tokenUrl = 'http://jgbim.qhfct.com:9000/'
export default {
  install: function (Vue, options) {
    if (DEV_FLAG) {
      Vue.prototype.baseUrl = '/'
      Vue.prototype.tokenUrl = '/'
      store.commit("setToken", false)
    } else {
      Vue.prototype.baseUrl = prodUrl
      Vue.prototype.tokenUrl = tokenUrl
      store.commit("setToken", true)
    }
    Vue.prototype.SXDTPM_URL =
      Vue.prototype.baseUrl + 'qhszpm/servlet/Action.cmd'
    Vue.prototype.COMMON_URL =
      Vue.prototype.baseUrl + 'qhszpm/servlet/Action.cmd'
    Vue.prototype.LOGIN_URL =
      Vue.prototype.baseUrl + 'qhszpm/servlet/LoginServlet.cmd'
    Vue.prototype.FORM2SUBSYS_SUPPORT_URL =
      Vue.prototype.baseUrl + 'qhszpm/servlet/StreamSupportAction.cmd'
    Vue.prototype.TOKEN_URL = Vue.prototype.tokenUrl + "getToken"
    Vue.prototype.WEATHER_URL = 'https://www.tianqiapi.com'
    Vue.prototype.$ajax = instance
    Vue.prototype.downloadFile = downloadFile

    Vue.component('cell', Cell)
    Vue.component('check-icon', CheckIcon)
    Vue.component('datetime', Datetime)
    Vue.component('grid', Grid)
    Vue.component('grid-item', GridItem)
    Vue.component('cell-box', CellBox)
    Vue.component('group', Group)
    Vue.component('loading', Loading)
    Vue.component('load-more', LoadMore)
    Vue.component('popup', Popup)
    Vue.component('popup-picker', PopupPicker)
    Vue.component('popup-header', PopupHeader)
    Vue.component('popup-radio', PopupRadio)
    Vue.component('picker', Picker)
    Vue.component('tab', Tab)
    Vue.component('tab-item', TabItem)
    Vue.component('toast', Toast)
    Vue.directive('transfer-dom', TransferDom)
    Vue.component('x-circle', XCircle)
    Vue.component('x-header', XHeader)
    Vue.component('x-textarea', XTextarea)
    Vue.use(ConfirmPlugin)
    Vue.use(ToastPlugin)
    Vue.use(LoadingPlugin)
    Vue.use(AlertPlugin)
    Vue.component('simple-search', SimpleSearch)
    // Vue.component('panel', panel)
    Vue.component('photo-upload', PhotoUpload)
    Vue.component('picture-upload', PictureUpload)
    Vue.component('accordion', accordion)
    Vue.component('person-tree', PersonTree)
    Vue.component('dept-tree', DeptTree)
    Vue.component('detail-table', detailTable)
    Vue.component('side-bar', SideBar)
    Vue.use(VueScroller)
    Vue.component('icon', Icon)
    Vue.component('alert', Alert)
    Vue.component('x-button', XButton)
    Vue.component('swiper', Swiper)
    Vue.component('swiper-item', SwiperItem)
    Vue.component('x-progress', XProgress)
    Vue.component('default-button', defaultButton)
    Vue.component('choose-project', ChooseProject)
    Vue.component('select-project', SelectProject)
    Vue.component('project-root', ProjectRoot)

    Vue.prototype.getReqUrl = getReqUrl //��ȡ������ܲ���
    Vue.prototype.getTaskParams = getTaskParams //��ȡ��ͬ��������������
    Vue.prototype.getRiskParams = getRiskParams
    Vue.prototype.guid = guid //��ȡguid
    Vue.prototype.toTree = toTree //����ת������
    Date.prototype.pattern = pattern
    Vue.prototype.signMD5 = signMD5
    Vue.prototype.judgeDeviceType = judgeDeviceType
    Vue.prototype.checkNetWork = checkNetWork
    //ǿ�ƹر��ڸǲ�
    Vue.prototype.closeMask = function () {
      let mask = document.querySelector('.vux-popup-mask')
      mask.classList.remove('vux-popup-show')
      setTimeout(() => {
        mask && !/show/.test(mask.className) && (mask.style['zIndex'] = -1)
      }, 400)
      if (store.state.defaultPop) {
        store.commit('CHANGE_DEFAULT_POPUP')
      }
    }

    //   //�ǳ�
    //   Vue.prototype.loginOut = function () {
    //     this.$router.push('/')
    //   }

    //��ȡ�û�
    Vue.prototype.getPerson = function (username, type, prjId, callback) {
      if (checkNetWork()) {
        //console.log("正常工作！");
        this.$ajax({
          method: 'post',
          url: this.COMMON_URL,
          // +
          //   '?_method=V_USER_SELECT*getHRUserList&filter=&type=prj&projectId=0',
          data: {
            _method: "V_USER_SELECT*getHRUserList",
            type: "prj",
            projectId: '0',
            MiniQueryFormData: '{"RoleId":"","ChooseProjectId":"' + prjId + '","GroupId":"","UserFullName":"' +
              username +
              '"}',
            pageIndex: '0',
            pageSize: '1000',
            sortField: 'USER_FULLNAME',
            sortOrder: 'asc'
          }
        })
          .then(res => {

            callback(res)
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        //执行离线状态时的任务   
        var temp = [];
        store.state.SQLiteDB.transaction((tx) => {
          tx.executeSql('SELECT * FROM PERSON_TREE WHERE USER_NAME LIKE "%' + username + '%"', [], (tx, result) => {

            if (result.rows) {
              var length = result.rows.length
              for (var i = 0; i < length; i++) {
                temp[i] = result.rows.item(i)
              }
              callback(temp)
            }
          });
        }, (err) => {
          console.log('错误', err)
        })
      }
    }
    //选人控件定位人员（UserFullName在后台对应username,传入参数为username）
    Vue.prototype.locatePerson = function (username, type, prjId, callback) {
      if (checkNetWork()) {
        //console.log("正常工作！");
        this.$ajax({
          method: 'post',
          url: this.COMMON_URL,
          //+ '?_method=V_USER_SELECT*locateUserForMobile&filter=&type=prj&projectId=0',
          data: {
            _method: "V_USER_SELECT*locateUserForMobile",
            type: "prj",
            projectId: '0',
            MiniQueryFormData: '{"RoleId":"","ChooseProjectId":"' + prjId + '","GroupId":"","UserFullName":"' +
              username +
              '"}',
            pageIndex: '0',
            pageSize: '1000',
            sortField: 'USER_FULLNAME',
            sortOrder: 'asc'
          }
        })
          .then(res => {
            console.log("renyuanhhh", res);
            callback(res)
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        //执行离线状态时的任务   
        var temp = [];
        store.state.SQLiteDB.transaction((tx) => {
          tx.executeSql('SELECT * FROM PERSON_TREE WHERE USER_NAME = "' + username + '"', [], (tx, result) => {

            if (result.rows) {
              var length = result.rows.length
              for (var i = 0; i < length; i++) {
                temp[i] = result.rows.item(i)
              }
              callback(temp)
            }
          });
        }, (err) => {
          console.log('错误', err)
        })
      }
    }

    // ��ȡtoken
    // Vue.prototype.getToken = function(username, password) {
    //     this.$http({
    //             method: 'post',
    //             url: TOKEN_URL + "?username=" + username + "&password=" + password,
    //             data: {}
    //         }).then((res) => {
    //             if (res.data.code == 200) {
    //                 window.localStorage.setItem("token", res.data.msg);
    //                 this.$store.dispatch('getUserInfo')
    //                 this.$store.dispatch('getNews')
    //                 this.$store.dispatch('getDept', {
    //                     id: 0
    //                 })
    //             }
    //         })
    //         .catch((err) => {
    //             console.log(err)
    //         })
    // }

    //��ʽ���ʱ�׼ʱ���ʽΪ200-mm-dd �Ż����ں�ʱ�� ����
    //   Vue.prototype.formatDatee = formatDatee
    //   Vue.prototype.formatDate = formatDate
    //�ж϶���Ϊ��
    Vue.prototype.isEmptyObject = e => {
      var t
      for (t in e) return !1
      return !0
    }
  }
}
